package ajax.quanxuan.sys.controller;


import ajax.quanxuan.sys.entity.Log;
import ajax.quanxuan.sys.entity.Notice;
import ajax.quanxuan.sys.entity.User;
import ajax.quanxuan.sys.service.NoticeService;
import ajax.quanxuan.sys.utils.SystemConstant;
import ajax.quanxuan.vo.DataGridViewResult;
import ajax.quanxuan.vo.JSONResult;
import ajax.quanxuan.vo.LogVo;
import ajax.quanxuan.vo.NoticeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/sys/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    /**
     * 查询公告列表
     *
     * @param noticeVo
     * @return
     */
    @RequestMapping("noticelist")
    public DataGridViewResult loglist(NoticeVo noticeVo) {
        //创建分页信息  参数  当前页  每页显示数量
        IPage<Notice> page = new Page<Notice>(noticeVo.getPage(), noticeVo.getLimit());
        //创建条件构造器
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>();
        //标题
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()), "title", noticeVo.getTitle());
        //发布人
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()),
                "opername", noticeVo.getOpername());
        //开始时间
        queryWrapper.ge(noticeVo.getStartTime() != null, "createtime", noticeVo.getStartTime());
        //结束时间
        queryWrapper.le(noticeVo.getEndTime() != null, "createtime", noticeVo.getEndTime());
        //根据创建时间排序
        queryWrapper.orderByDesc("createtime");
        //调用查询日志列表的方法
        noticeService.page(page, queryWrapper);
        //返回数据
        return new DataGridViewResult(page.getTotal(), page.getRecords());
    }

    @PostMapping("addNotice")
    public JSONResult addNotice(Notice notice, HttpSession session) {
        User user = (User) session.getAttribute(SystemConstant.LOGINUSER);
        notice.setCreatetime(new Date());
        notice.setOpername(user.getName());//发布人
        if (noticeService.save(notice)) {
            //添加成功
            return SystemConstant.ADD_SUCCESS;
        }
        //添加失败
        return SystemConstant.ADD_ERROR;

    }

    /**
     * 修改公告
     * @param notice
     * @param session
     * @return
     */

    @PostMapping("updateNotice")
    public JSONResult updateNotice(Notice notice, HttpSession session) {
        notice.setModifytime(new Date());
        if (noticeService.updateById(notice)){
            //如果修改成功
            return SystemConstant.UPDATE_SUCCESS;
        }
        //如果修改失败
        return SystemConstant.UPDATE_ERROR;
    }
    @RequestMapping("deleteById")
    public JSONResult deleteById(int id){
        if (noticeService.removeById(id)){
            //如果成功
            return SystemConstant.DELETE_SUCCESS;
        }
        //如果失败
        return SystemConstant.DELETE_ERROR;
    }
}
